package info.javatech.jmf.client.util;

import info.javatech.jmf.client.controller.wsclient.PComponent;

public class PComponentUtils {
    
    /** Creates a new instance of Utils */
    
    public static String getStringProperty(PComponent component, String propertyName) throws InvalidComponentException {
        if (
                component==null||
                component.getPropertyNames()==null||
                component.getPropertyValues()==null||
                component.getPropertyNames().length!=component.getPropertyValues().length)
            throw new InvalidComponentException();
        
        if (propertyName == null) return null;
        
        for(int i = 0;i<component.getPropertyNames().length;i++)
            if (propertyName.equals(component.getPropertyNames()[i]))
                return component.getPropertyValues()[i];
        
        return null;
    }
    
    public static int getIntProperty(PComponent component, String propertyName) throws InvalidComponentException {
        String str = getStringProperty(component, propertyName);
        if (str == null||str.equals("")) return 0;
        int value = 0;
        try {
            return Integer.valueOf(str).intValue();
        } catch (NumberFormatException ex) {
            throw new InvalidComponentException();
        }
    }
    
    public static void setStringProperty(PComponent component, String propertyName, String propertyValue) throws InvalidComponentException {
        if (
                component==null||
                component.getPropertyNames()==null||
                component.getPropertyValues()==null||
                component.getPropertyNames().length!=component.getPropertyValues().length)
            throw new InvalidComponentException();
        
        // TODO maybe it needs to create different exception type
        if (propertyName == null) throw new InvalidComponentException();
        
        boolean found = false;
        for(int i = 0;i<component.getPropertyNames().length;i++)
            if (propertyName.equals(component.getPropertyNames()[i]))
                component.getPropertyValues()[i] = propertyValue;
    }
}
